clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"


*** Select zip_codes  
use "./cll_arra.dta", clear
drop if inlist(state_id, "PR", "AK", "HI")

rename fipscode county
merge 1:m county using "ZIP_COUNTY_032010.dta", keep(master matched) nogen keepusing(prop_zip_code res_ratio)

sort prop_zip_code county 
by prop_zip_code: egen highcost_ratio = total(res_ratio * (cll > 517000))
by prop_zip_code: egen cll_min = min(cll)
by prop_zip_code: egen cll_max = min(cll)

keep if cll_max > 417000

keep prop_zip_code highcost_ratio state_id cll_m*
duplicates drop prop_zip_code, force


// merge with lps
merge 1:m prop_zip_code using "${embsRaw}/lps-hibal.dta", keep(matched) nogen

gen temp = date(orig_date, "YMD")
drop orig_date
rename temp orig_date

gen year = year(orig_date)
keep if year <= 2018

gen qtr = qofd(orig_date)
format qtr %tq

gen month = mofd(orig_date)
format month %tm

gen fulldoc = document_type_id == "1"


*keep if loan_to_value <= 80

gen gse_id =  inlist(investor_type_id, "2", "3")==1
bys mcdash_id: egen temp = max(gse_id)
replace gse_id = temp 
gen nogse_id = 1- gse_id


sort mcdash_id min_date
by mcdash_id: keep if _n==_N

keep if year <= 2018
keep if loan_to_value <= 80


foreach x of varlist appraisal_amount current_int_rate fico loan_to_value orig_amount {
	qui su `x', d
	replace `x' = . if inrange(`x', `r(p1)', `r(p99)')==0
}

// regression

gen cll = 417000
replace cll = 424100 if year == 2017
replace cll = 453100 if year == 2018

gen PaboveC =  (appraisal_amount - (cll/.8)) / 1000

gen HiP = appraisal_amount  > (cll/ .8)
gen HiBal = orig_amount > cll

drop if loan_src_type_id == "U"
egen loan_src_id = group(loan_src_type_id)

qui do "${code}/tba_elig/_LLPAbins.do" fico loan_to_value
egen LLPAbin = group(credit_score_bin ltv_bin)

gen PaboveC1 = PaboveC
gen PaboveC2 = PaboveC * PaboveC 
gen PaboveC3= PaboveC2* PaboveC
gen PaboveC4= PaboveC3* PaboveC  

global poly1 "PaboveC 1.HiP#c.PaboveC1"
global poly2 "$poly1 PaboveC2 1.HiP#c.PaboveC2"
global poly3 "$poly2 PaboveC3 1.HiP#c.PaboveC3"


global controls "c.fico##c.fico##c.fico c.debt_to_income_ratio##c.debt_to_income_ratio##c.debt_to_income_ratio i.loan_src_id fnm"
gen fnm = investor_type_id == "2"

gen bin = ceil(PaboveC/2.5)*2.5
label var bin "Appraisal Value - 1.25 CLL ($1000)"

*** 2. Whether to be sold to GSE
keep if abs(PaboveC)<=150
global combine ""
foreach y of numlist 2009/2018 {
	
	reghdfe nogse_id HiP $poly3 $controls if  year == `y', a(FEzip=i.prop_zip_code FEym=i.month FEllpa=i.LLPAbin) vce(robust)
	est save "${Estimates}/lps-nogse_id-`y'", replace

	predict resid_nogse_id, r 
	foreach x in HiP $poly3 {
		replace resid_nogse_id = resid_nogse_id + _b[`x']* (`x')
	}
	
	local name "${temp}/rdplot-lps-nogse_id-`y'.gph"
	global combine "$combine `name'"
	
	preserve
		keep if year == `y'
		collapse resid_nogse_id, by(bin) fast
		twoway scatter resid_nogse_id bin, xline(0) xtitle("") ytitle("") ylabel(, angle(0)) xlabel(-150(50)150) ///
			saving("`name'", replace) title(`y') 
	restore
	drop FE* 
	drop resid_nogse_id
}
graph combine $combine  
graph export "${figure}/rdplot-lps-nogse_id.pdf", replace

*** 3. Create a table
clear
cd "${code}/tba_elig"
run _esttab-latex-options.do

set obs 1
gen PaboveC = 1
gen nogse_id = 1
gen HiP = 1

eststo clear
local mtitle ""
foreach y of numlist 2009/2018 {
	

	local mtitle "`mtitle' `y'"	  
	estimate use "${Estimates}/lps-nogse_id-`y'"
	eststo
	
	quietly {
		estadd local Zip "Y"
		estadd local Month "Y"
		estadd local control "Y"
		estadd local llpa "Y"
	}
}
run "${code}/_labels"		
esttab using "${table}/lps-nogse_id.tex", replace keep(HiP 1.HiP#c.PaboveC1) coeflabels(1.HiP#c.PaboveC1 "$ z_i\times 1[z_i>0]$") ///
 ${opts_esttab}  b(%9.3fc) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
 stats(llpa Zip Month control N r2_a, fmt(%1s %1s %1s %1s %9.0fc %3.2f) ///
 labels("LLPA FE" "Zipcode FE" "Month FE" "Other Controls" "\midrule N. Obs." "Adj. $ R^2$")) ///
 mtitle(`mtitle')


